package com.sm42.goodsService.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sm42.baseService.impl.BaseCrmpServiceImpl;
import com.sm42.goodsEntity.Product;
import com.sm42.goodsService.ProductService;
import com.sm42.mapper.goodsMapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImpl extends BaseCrmpServiceImpl<Product> implements ProductService {
    @Autowired
    private ProductMapper productMapper;


    @Override
    public List<Product> findProductCoding(String productCoding) {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("product_coding",productCoding);
        List<Product> products = productMapper.selectList(wrapper);
        return products;
    }

    @Override
    public List<Product> findProductType(String productType) {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("product_type",productType);
        List<Product> products = productMapper.selectList(wrapper);
        return products;
    }

    @Override
    public Product findId(Long id) {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("product_id", id);
        Product product = productMapper.selectOne(wrapper);
        return product;
    }

//    @Override
//    public List<Product> findAllList(int page,int pageInfo) {
//        Page<Product> page1 = new Page<>(page, pageInfo);
//        Page<Product> productPage = productMapper.selectPage(page1, null);
//        productPage.getTotal();
//        return productPage.getRecords();
//    }

//    @Override
//    public List<Product> findProductList(Product product) {
//        QueryWrapper<Product> wrapper = new QueryWrapper<>();
//        wrapper.eq("product_coding",product.getProductCoding()).or()
//                .eq("product_class",product.getProductClass()).or()
//                .eq("product_brand",product.getProductBrand()).or()
//                .eq("product_type",product.getProductType()).or()
//                .eq("product_color",product.getProductColor());
//        List<Product> products = productMapper.selectList(wrapper);
//        return products;
//    }

    @Override
    public List<Product> findSearch(Product product) {
        System.out.println(product);
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq(product.getProductId() != 0,"product_id",product.getProductId())
                .like(ObjectUtils.isNotNull(product.getProductCoding()),"product_coding",product.getProductCoding())
                .like(ObjectUtils.isNotNull(product.getProductClass()),"product_class",product.getProductClass())
                .like(ObjectUtils.isNotNull(product.getProductBrand()),"product_brand",product.getProductBrand())
                .like(ObjectUtils.isNotNull(product.getProductType()),"product_type",product.getProductType())
                .like(ObjectUtils.isNotNull(product.getProductColor()),"product_color",product.getProductColor());
        List<Product> products = productMapper.selectList(wrapper);
        return products;
    }

    @Override
    public int deleteProduct(Long id) {
        return deleteById(id);
    }

    @Override
    public int updateProduct(Product product) {
        product.setUpdateTime(LocalDateTime.now());
        return productMapper.updateById(product);
    }

    @Override
    public int addProduct(Product product) {
        product.setUpdateTime(LocalDateTime.now());
        return add(product);
    }
}
